﻿using System.Windows.Documents;
using System.Windows.Media;
using CommunityToolkit.Mvvm.Messaging;
using MG.SmartHMI.Common;
using MG.Util.Log;

namespace MG.SmartHMI.View
{
    /// <summary>
    /// HomeView.xaml 的交互逻辑
    /// </summary>
    public partial class HomeView : System.Windows.Controls.UserControl
    {
        public HomeView()
        {
            InitializeComponent();
            Register();
        }

        private void Register()
        {
            EventBus.Default.BindLogAction(WriteUILogSlot);
            WeakReferenceMessenger.Default.Register<string, string>(this, "ClearUILogSignal", ClearUILogSlot);
        }

        private void ClearUILogSlot(object recipient, string str)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                rbx_log.Document.Blocks.Clear();
            });
        }

        /// <summary>
        /// 向界面输出log
        /// </summary>
        /// <param name="log"></param>
        private void WriteUILogSlot(LogInfo log)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                var logRun = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{log.LogLevel,-5}]：{log.Content}";
                Brush brush;
                switch (log.LogLevel)
                {
                    case LogLevel.Trace:
                        return;

                    case LogLevel.Info:
                        brush = Brushes.Black;
                        break;

                    case LogLevel.Debug:
                        brush = Brushes.Blue;
                        break;

                    case LogLevel.Warn:
                        brush = Brushes.Orange;
                        break;

                    case LogLevel.Fatal:
                    case LogLevel.Error:
                        brush = Brushes.Red;
                        break;

                    default:
                        brush = Brushes.Black;
                        break;
                }
                if (rbx_log.Document.Blocks.Count >= 500)
                {
                    ClearUILogSlot(null, null);
                }
                rbx_log.Document.Blocks.Add(new Paragraph(new Run(logRun) { Foreground = brush }));
                rbx_log.ScrollToEnd();
            });
        }

        private void Page_Loaded(object sender, System.Windows.RoutedEventArgs e) => WeakReferenceMessenger.Default.Send<string, string>(string.Empty, "HomeLoadSlotSignal");

        private void Page_Unloaded(object sender, System.Windows.RoutedEventArgs e) => WeakReferenceMessenger.Default.Send<string, string>(string.Empty, "HomeUnLoadSlot");

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            WeakReferenceMessenger.Default.Send(string.Empty, "HomeViewLoadSignal");
        }

        private void UserControl_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            WeakReferenceMessenger.Default.Send(string.Empty, "HomeViewUnLoadSignal");
        }

        private void Btn_ClearLog_Event(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                rbx_log.Document.Blocks.Clear();
            });
        }
    }
}